﻿using System;
using System.Threading;
using System.Threading.Tasks;
using K9Nano.Dependency;
using K9Nano.Domain;
using K9Nano.Share.Exceptions;
using K9Nano.Share.Extensions;
using Microsoft.Extensions.Logging;

namespace K9Nano.Authorization
{
    public class UserBlacklistChecker : K9DomainServiceBase, IUserBlacklistChecker, IScopedDependency
    {
        public UserBlacklistChecker(IUnitOfWork uow, ILoggerFactory loggerFactory) : base(uow, loggerFactory)
        {
        }

        public virtual async ValueTask<bool> IsForbiddenAsync(string idOrPhone, string clientId, CancellationToken cancellation)
        {
            return await Uow.Repository<UserBlacklist, Guid>()
                .AnyAsync(x => x.User == idOrPhone
                               && (x.Policy == EUserBlackListPolicy.Entirely || x.Clients.Contains(clientId)), cancellation: cancellation);

        }
    }
}